<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="订单ID" v-bind="validateInfos.orderId">
            <a-input v-model:value="formData.orderId" placeholder="请输入订单ID" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="生产计划编号" v-bind="validateInfos.code">
            <a-input v-model:value="formData.code" placeholder="请输入生产计划编号" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="产品名称" v-bind="validateInfos.orderProduct">
            <a-input v-model:value="formData.orderProduct" placeholder="请输入产品名称" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="生产量（L）" v-bind="validateInfos.orderNum">
	          <a-input-number v-model:value="formData.orderNum" placeholder="请输入生产量" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="生产状态" v-bind="validateInfos.processStatus">
	          <a-input-number v-model:value="formData.processStatus" placeholder="请输入生产状态" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="原料" v-bind="validateInfos.materialIds">
	          <j-select-multiple type="list_multi" v-model:value="formData.materialIds" dictCode="camellia_mes_raw,name,id" placeholder="请选择原料" disabled :triggerChange="false"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="工艺" v-bind="validateInfos.crafts">
	          <j-select-multiple type="list_multi" v-model:value="formData.crafts" dictCode="camellia_mes_process_craft,craft_name,id" placeholder="请选择工艺" disabled :triggerChange="false"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="机器" v-bind="validateInfos.machines">
	          <j-select-multiple type="list_multi" v-model:value="formData.machines" dictCode="manage_device,device_name,id" placeholder="请选择机器" disabled :triggerChange="false"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="生产员工" v-bind="validateInfos.executor">
	          <j-select-multiple type="list_multi" v-model:value="formData.executor" dictCode="camellia_mes_empployee,name,id" placeholder="请选择生产员工" disabled :triggerChange="false"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="负责人" v-bind="validateInfos.principal">
	          <j-select-multiple type="list_multi" v-model:value="formData.principal" dictCode="camellia_mes_empployee,name,id" placeholder="请选择负责人" disabled :triggerChange="false"/>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24">
          <a-form-item label="用时（L）" v-bind="validateInfos.timeRequired">
	          <a-input-number v-model:value="formData.timeRequired" placeholder="请输入计划用时" style="width: 100%" disabled/>
          </a-form-item>
        </a-col> -->
        <a-col :span="24">
          <a-form-item label="预计开始时间" v-bind="validateInfos.estStartTime">
		        <a-date-picker placeholder="请选择预计开始时间"  v-model:value="formData.estStartTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="预计结束时间" v-bind="validateInfos.estEndTime">
		        <a-date-picker placeholder="请选择预计结束时间"  v-model:value="formData.estEndTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24">
          <a-form-item label="实际开始时间" v-bind="validateInfos.realStartTime">
		        <a-date-picker placeholder="请选择实际开始时间"  v-model:value="formData.realStartTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="实际结束时间" v-bind="validateInfos.realEndTime">
		        <a-date-picker placeholder="请选择实际结束时间"  v-model:value="formData.realEndTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
          </a-form-item>
        </a-col> -->
        <a-col :span="24">
          <a-form-item label="产品产量（L）" v-bind="validateInfos.orderNum">
		        <a-input-number placeholder="请输入产品产量"  v-model:value="formData.orderNum" style="width: 100%" :disabled="disabled" addon-before="+"></a-input-number>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="副产品产量（kg)" v-bind="validateInfos.byproductNum">
            <a-input-number placeholder="请输入副产品产量"  v-model:value="formData.byproductNum"  style="width: 100%" :disabled="disabled" addon-before="+"></a-input-number>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JSelectMultiple from '/@/components/Form/src/jeecg/components/JSelectMultiple.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../CamelliaMesProcessPlan.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    orderId: '',   
    code: '',   
    orderProduct: '',   
    orderNum: undefined,
    processStatus: undefined,
    timeRequired: undefined,
    materialIds: '',   
    crafts: '',   
    machines: '',   
    executor: '',   
    principal: '',   
    estStartTime: '',   
    estEndTime: '',   
    realStartTime: '',   
    realEndTime: '',   
    productYield: '',
    byproductNum: ''
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    // await saveOrUpdate(model, isUpdate.value)
    //   .then((res) => {
    //     if (res.success) {
    //       createMessage.success(res.message);
    //       emit('ok');
    //     } else {
    //       createMessage.warning(res.message);
    //     }
    //   })
    //   .finally(() => {
    //     confirmLoading.value = false;
    //   });

    console.log(model);

  let url = '/mes/myplan/end_produce';
  let params = model;
  delete params.machinesOptions;
  await defHttp
    .post({ url, params })
    .then((res) => {
      //console.log(res)
      // if (res.code == 200) {
      //createMessage.success(res.message);
      emit('ok');
      //   } else {
      //     createMessage.warning(res.message);
      //   }
    })
    .finally(() => {
      confirmLoading.value = false;
    });

  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
